package com.iofile.utils;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;


@SuppressWarnings("all")
public class RestTemplateUtils {

    private static RestTemplate restTemplate = new RestTemplate();

    public static String uploadFileByBytes(String url, byte[] data) {
        ByteArrayResource byteArrayResource = new ByteArrayResource(data){
            @Override
            public String getFilename() {return "audio.wav"; }
        };
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        // 设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", byteArrayResource);

        // 用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        return restTemplate.postForObject(url, files, String.class);
    }

    /**
     * 特定人注册
     * @param url
     * @param data
     * @return
     */
    public static String register(String personName,String url, byte[] data) {
        ByteArrayResource byteArrayResource = new ByteArrayResource(data){
            @Override
            public String getFilename() {return "audio.wav"; }
        };
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        // 设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", byteArrayResource);
        form.add("speakerName", personName);

        // 用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
        return restTemplate.postForObject(url, entity, String.class);
    }

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String speakerScope(String url, String personNames) {
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        // 设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("targetList", personNames);

        // 用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
        return restTemplate.postForObject(url, entity, String.class);
    }

    public static String getSpeaker(String url, byte[] data) {
        ByteArrayResource byteArrayResource = new ByteArrayResource(data){
            @Override
            public String getFilename() {return "audio.wav"; }
        };
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        // 设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", byteArrayResource);
        // 用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
        return restTemplate.postForObject(url, entity, String.class);
    }

    public static String updateSpeaker(String url, String speakerName, String data) {

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        // 设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("speakerName", speakerName);
        form.add("features", data);

        // 用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
        return restTemplate.postForObject(url, entity, String.class);
    }
}
